var regContextPath = $('#regJsContext').val();
var $usercenterUrl = "http://member.chinaacc.com/uc"
$(function() {
    $("#ch_reg_mobile").attr("data-flag", "");
    $("#ch_reg_pwd").attr("data-flag", "");
    $("#ch_reg_psms").attr("data-flag", "");
    $("#ch_reg_sms").attr("data-flag", "");
    $(".chaaTcLRD_reg_btn").attr("data-flag", "");
    $("#ch_reg_smsBtn").attr("data-flag", "");
    $("#ch_reg_imgPms").attr("src",
        $usercenterUrl + "/randcode?codeType=randCode&_r=" + Math.random());
    $("#ch_reg_mobile").blur(function() {
        checkPhoneStatus($(this));
        checkMobilePhone($(this));
    });
    $("#ch_reg_pwd").blur(function() {
        checkPwd($(this));
    });
    $("#ch_reg_psms").blur(function() {
        checkPicCode($(this));
    });
    $("#ch_reg_sms").blur(function() {
        checkSmsCode($(this));
    }).keyup(function() {
        checkSmsCode($(this));
    });
    $("#ch_reg_smsBtn").click(function() {
        toSmsCode();
    });
    $(".chaaTcLRD_reg_btn").click(function() {
        var dataFlag = $("#ch_reg_mobile").attr("data-flag");
        var $mobilePhone = $("#ch_reg_mobile").val();
        if ($mobilePhone == "" || $mobilePhone == "手机号，将作为您的学员代码，可直接用于登录") {
            $("#ch_reg_mobile").attr("data-flag", "");
            errorloretip($("#ch_reg_mobile"), "请输入手机号");
        }
        var pwdFlag = checkPwd($("#ch_reg_pwd"));
        var smsFlag = checkSmsCode($("#ch_reg_sms"));
        var picFlag = checkPicCode($("#ch_reg_psms"));
        if ((dataFlag == '1') && pwdFlag && smsFlag && picFlag) {
            regSubmit($(this));
        }

    });
    $("#ch_reg_imgPms").click(function() {
        chgVerify($("#ch_reg_mobile").val());
    })
});
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
function checkPwd(obj) {
    var $passwd = $.trim(obj.val());
    var $flag = false;
    var $passwordRegex = /^[a-zA-Z0-9]{6,15}$/;
    if ($passwd == "" || $passwd == "密码，6-15位，数字，字母组合") {
        $flag = false;
        $("#ch_reg_pwd").attr("data-flag", "")
        errorloretip(obj, "请输入密码")
    } else {
        if (!$passwordRegex.test($passwd)) {
            $flag = false;
            errorloretip(obj, "密码，6-15位，数字，字母组合");
        } else {
            $flag = true;
            errorloretip(obj);

        }
    }
    return $flag;
}

function checkPhoneStatus(obj) {
    var $mobilePhone = $.trim(obj.val());
    var $regexMobile = /^1[3|4|5|7|8][0-9]\d{8}$/;
    if ($mobilePhone == "" || $mobilePhone == "手机号，将作为您的学员代码，可直接用于登录") {
        $("#ch_reg_mobile").attr("data-flag", "");
        return false;
    } else {
        if (!$regexMobile.test($mobilePhone)) {
            errorloretip(obj, "请输入正确的手机号");
            return false;
        } else {
            $.ajax({
                type : "POST",
                url : regContextPath + '/api/member/regCheckPhoneInBook.shtm',
                data : {
                    url : $usercenterUrl + "/checkPhoneInBook",
                    phone : $mobilePhone,
                    time : Math.random()
                },
                dataType : 'json',
                success : function(json) {
                    var $code = $.trim(json.code);
                    if ($code == "-1") {// 需要输入验证码，验证码显示。
                        $("#imgPmsCode").show();
                        chgVerify($mobilePhone);
                    } else {
                        // 无需输入验证码，不显示验证码
                    }
                }
            })
        }
    }
    return true;
}
function checkMobilePhone(obj) {
    var $mobilePhone = $.trim(obj.val());
    var $regexMobile = /^1[3|4|5|7|8][0-9]\d{8}$/;
    if ($mobilePhone == "" || $mobilePhone == "手机号，将作为您的学员代码，可直接用于登录") {
        $("#ch_reg_mobile").attr("data-flag", "");
        errorloretip(obj, "请输入手机号");
        return false;
    } else {
        if (!$regexMobile.test($mobilePhone)) {
            errorloretip(obj, "请输入正确的手机号");
            return false;
        } else {
            $.ajax({
                type : "POST",
                url : regContextPath + '/api/member/regIfMobBound.shtm',
                data : {
                    url : $usercenterUrl + "/api/ifMobileBound",
                    phone : $mobilePhone,
                    time : Math.random()
                },
                dataType : 'json',
                async : false,
                success : function(json) {
                    var $code = $.trim(json.code);
                    if ($code == "-5") {
                        errorloretip(obj);
                        $("#ch_reg_mobile").attr("data-flag", "1");
                        $("#ch_reg_smsBtn").removeAttr("disabled");
                    } else {
                        if ($code == "-2") {
                            errorloretip(obj, "该手机号已被注册，可以直接登录");
                        } else {
                            errorloretip(obj, "您输入的手机号不允许绑定");
                        }
                        $("#ch_reg_mobile").attr("data-flag", "");
                        return false;
                    }
                },
                error : function(data) {
                    $("#ch_reg_mobile").attr("data-flag", "");
                    errorloretip(obj, "网络连接超时，请您稍后重试")
                    return false;
                }
            })
        }
    }
    return true;
}
function checkPicCode(obj) {
    var randcodeDisplay = $("#imgPmsCode").is(":visible");
    if (randcodeDisplay) {
        var $picCode = $.trim(obj.val());
        if ($picCode == "" || $picCode == "请输入验证码") {
            errorloretip(obj, "请输入验证码");
            obj.attr("data-flag", "")
            $("#ch_reg_smsBtn").removeAttr("disabled");
            return false;
        } else {
            errorloretip(obj);
            obj.attr("data-flag", 1);
            $("#ch_reg_smsBtn").removeAttr("disabled");
            return true;
        }
    }
    return true;
}
function checkSmsCode(obj) {
    var $smsCode = $.trim(obj.val());
    tips = "短信";
    if (djscount >= 2) {
        tips = "语音";
    }
    if ($smsCode == "" || $smsCode == "请输入" + tips + "验证码") {
        errorloretip(obj, "请输入" + tips + "验证码");
        $("#ch_reg_sms").attr("data-flag", "")
        return false;
    } else {
        errorloretip(obj);
        return true;
    }
}
function toSmsCode() {
    $("#ch_reg_smsBtn").attr("disabled", true);
    var $pCodeObj = $("#ch_reg_psms");
    var $sCodeObj = $("#ch_reg_sms");
    var $mobileObj = $("#ch_reg_mobile");
    var $pCode = $.trim($pCodeObj.val());
    var $mobile = $.trim($mobileObj.val());
    var $dataMap = null;
    if ($mobileObj.attr("data-flag") != 1) {
        if ($mobileObj.val() == "") {
            errorloretip($mobileObj, "请输入手机号")
        } else {
            checkMobilePhone($mobileObj);
        }
        return;
    }
    // 有验证码则进行验证码的校验
    if (!checkPicCode($pCodeObj)) {
        return;
    }
    if ($("#ch_reg_smsBtn").attr("data-flag") == 1) {
        $dataMap = {
            url : $usercenterUrl + "/smscode",
            phone : $mobile,
            randcode : $pCode,
            type : 1,
            codeType : 'randCode',
            time : Math.random()
        }
    } else {
        $dataMap = {
            url : $usercenterUrl + "/smscode",
            phone : $mobile,
            randcode : $pCode,
            codeType : 'randCode',
            clientID : getCookie('clientID'),
            paramUrl : $usercenterUrl,
            time : Math.random()
        }
    }
    $.ajax({
        type : "POST",
        url : regContextPath + '/api/member/regSmsCode.shtm',
        data : $dataMap,
        dataType : 'json',
        success : function(json) {
            var $code = $.trim(json.rtn);
            var $msg = $.trim(json.msg);
            if ($code == "1") {
                chgVerify($mobile);
                time($("#ch_reg_smsBtn"));
            } else {
                if ($code == "-1") {
                    $("#ch_reg_smsBtn").removeAttr("disabled");
                    errorloretip($mobileObj, $msg);
                    chgVerify($mobile);
                } else {
                    if ($code == "-3") {
                        $("#ch_reg_smsBtn").removeAttr("disabled");
                        errorloretip($pCodeObj, $msg);
                        $pCodeObj.attr("data-flag", "");
                        chgVerify($mobile);
                    } else if ($code == 107 || $code == 10002 || $code == 10003
                        || $code == 10004 || $code == 10005) {
                        $("#ch_reg_smsBtn").removeAttr("disabled");
                        errorloretip($sCodeObj, $msg);
                        $("#imgPmsCode").show();
                        chgVerify($mobile);
                    } else {
                        $("#ch_reg_smsBtn").removeAttr("disabled");
                        chgVerify($mobile);
                    }
                }
            }
        }
    })
}

function regSubmit(obj) {
    var $ch_reg_mobile = $("#ch_reg_mobile").val();
    var $ch_reg_pwd = $("#ch_reg_pwd").val();
    var $ch_reg_sms = $("#ch_reg_sms").val();
    var refUrl = $("#refUrl").val();
    $(".chaaTcLRD_reg_btn").text("正在注册...");
    $(".chaaTcLRD_reg_btn").attr("disabled", "disabled");
    $.ajax({
        type : "POST",
        url : "//member.chinaacc.com/member/wwwRegisterDisposeNew.shtm",
        data : {
            mobilePhone : $ch_reg_mobile,
            passwd : $ch_reg_pwd,
            smsCode : $ch_reg_sms,
            appRegUrl : refUrl
        },
        dataType : "jsonp",
        jsonp : "jsonpCallback",
        jsonpCallback : "success_jsonpCallback",
        success : function(json) {
            var $code = $.trim(json.code);
            var $msg = $.trim(json.msg);
            var $domId = $.trim(json.domId);
            var $v_id = $.trim(json.v_id);
            if ($code == "0") {
                lm($v_id);
                $(".add_getSms_tip").hide();
                closelore();
                clearIptData();
                onloadW();
            } else {
                if ($code == "-12") {
                    $(".chaaTcLRD_reg_btn").text("注 册");
                    $(".chaaTcLRD_reg_btn").prev('span').text($msg);
                } else {
                    $(".chaaTcLRD_reg_btn").text("注 册");
                    if ("ch_reg_sms" == $domId) {
                        $msg = $msg.replace("短信", tips);
                    }
                    errorloretip($("#" + $domId), $msg);
                }
                $(".chaaTcLRD_reg_btn").removeAttr("disabled");
            }
        }
    })
}
function errorloretip(obj, text) {
    if (!text) {
        obj.attr("data-flag", 1).parents(
            '.chaaTcLRD_d,.chaaTcLRD_dpsms,.chaaTcLRD_dsms_in')
            .removeClass('error');
        obj.parent('div').siblings('.error_tip').hide();
        obj.parent('div').siblings('.succ_tip').css({
            "display" : "inline"
        });
    } else {
        obj.attr("data-flag", "").parents(
            '.chaaTcLRD_d,.chaaTcLRD_dpsms,.chaaTcLRD_dsms_in').addClass(
            'error');
        obj.parent('div').siblings('.error_tip').show().html(text);
        obj.parent('div').siblings('.succ_tip').css({
            "display" : ""
        });
    }
}
function chgVerify(phone) {
    $("#ch_reg_imgPms").attr(
        "src",
        $usercenterUrl + "/randcode?codeType=randCode&phone=" + phone
        + "&_r=" + Math.random());
}
function clearIptData() {
    $(".chaaTcLRD_reg_btn").text("注 册").attr("data-flag", "").removeClass("on");
    $(".chaaTcLR_zc_in input").each(
        function(i, n) {
            $(n).val("");
            if ($(n).attr("id") == "ch_reg_psms") {
                $(n).parent("div").parent("div").siblings("div")
                    .removeClass("ews_tips_on")
            } else {
                $(n).parent("div").siblings("div").removeClass(
                    "ews_tips_on")
            }
        })
}
function closelore() {
    $(".chaaTcLR_dlzc").hide()
}
// 兼容低版本ie
$(function() {
    if ((navigator.appName == "Microsoft Internet Explorer")
        && (document.documentMode < 10 || document.documentMode == undefined)) {
        var $placeholder = $("input[placeholder]");
        for (var i = 0; i < $placeholder.length; i++) {
            $placeholder.eq(i).siblings("label").text(
                $placeholder.eq(i).attr("placeholder")).show();
        }
        $placeholder.focus(function() {
            $(this).siblings("label").hide();
        }).blur(
            function() {
                if ($(this).val() == "") {
                    $(this).siblings("label").text(
                        $(this).attr("placeholder")).show();
                    ''
                }
            });
        $placeholder.siblings("label").click(function() {
            $(this).hide().next("input").focus();
        })
    }
});
// 注册界面聚焦效果
$('.chaaTcLR_zc_in input').focus(
    function() {
        var $this_obj = $(this);
        $this_obj.parents(
            '.chaaTcLRD_d,.chaaTcLRD_dpsms,.chaaTcLRD_dsms_in')
            .removeClass('error').addClass('success');
        $this_obj.parent('div').siblings('.error_tip').hide();
    }).blur(
    function() {
        var $this_obj = $(this);
        $this_obj.parents(
            '.chaaTcLRD_d,.chaaTcLRD_dpsms,.chaaTcLRD_dsms_in')
            .removeClass('success');
    });
/**
 * .keyup(function(){ var $this_obj=$(this); if($this_obj.val()!=''){
 * $this_obj.parent('div').siblings('.succ_tip').css({"display":"inline"}); } })
 *
 */
// 验证码获取60面点击
var wait = 60;
var djscount = 0;
// 验证码倒计时函数
function time(o) {
    if (wait == 0) {
        djscount++;
        o.removeAttr("disabled");
        if (djscount >= 2) {
            tips = "语音";
            o.text("语音验证码");
            $("#ch_reg_sms").attr("placeholder", "语音验证码");
            o.attr("data-flag", 1)
        } else {
            o.text("免费获取");
        }
        o.removeClass("disb").attr('data-count', djscount);
        wait = 60;
    } else {
        o.attr("disabled", "disabled");
        o.text(wait + " S");
        o.addClass("disb");
        wait--;
        setTimeout(function() {
            time(o)
        }, 1000)
    }
}